import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

public class AuthLicense {

  public static void main(String[] args) throws Exception {

    System.out.println(Base64.encodeBase64String("/home/dbc/systemInfo/sysInfo.dat".getBytes()));

    System.out.println(new String(Base64.decodeBase64("L2hvbWUvZGJjL3N5c3RlbUluZm8vc3lzSW5mby5kYXQ=")));

    File file = new File("/home/dbc/systemInfo/sysInfo.dat");
    BufferedReader bufIn = null;
    try {
      FileReader in = new FileReader(file);
      bufIn = new BufferedReader(in);
      // 内存流, 作为临时流
      CharArrayWriter tempStream = new CharArrayWriter();
      // 替换
      String line = null;

      while ((line = bufIn.readLine()) != null) {
        System.out.println(line);
        if (line.contains("volidit")) {
          // 替换每行中, 符合条件的字符串
          line = line.replaceAll("2019", "2090");
        }
        // 将该行写入内存
        tempStream.write(line);
        // 添加换行符
        tempStream.append(System.getProperty("line.separator"));
      }
      bufIn.close();
      // 将内存中的流 写入 文件
      try (FileWriter out = new FileWriter(file)) {
        tempStream.writeTo(out);
      }
      System.out.println("====done======");
    } catch (Exception e) {
      System.out.println("error .");
    } finally {
      if (bufIn != null) {
        try {
          bufIn.close();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

  }

  public static String encryptForString(String content, String password) {
    try {
      return Hex.encodeHexString(encrypt(content, password));
    } catch (Exception e) {
      System.out.println(e.getCause());
      return null;
    }
  }

  public static byte[] encrypt(String content, String password) {
    try {
      KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者

      kgen.init(128, new SecureRandom(password.getBytes()));// 利用用户密码作为随机数初始化出
      // 128位的key生产者
      //加密没关系，SecureRandom是生成安全随机数序列，password.getBytes()是种子，只要种子相同，序列就一样，所以解密只要有password就行

      SecretKey secretKey = kgen.generateKey();// 根据用户密码，生成一个密钥

      byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥，如果此密钥不支持编码，则返回
      // null。

      SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥

      Cipher cipher = Cipher.getInstance("AES");// 创建密码器

      byte[] byteContent = content.getBytes("utf-8");

      cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化为加密模式的密码器

      byte[] result = cipher.doFinal(byteContent);// 加密

      return result;

    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  public static byte[] decrypt(byte[] content, String password) {
    try {
      KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者
      kgen.init(128, new SecureRandom(password.getBytes()));
      SecretKey secretKey = kgen.generateKey();// 根据用户密码，生成一个密钥
      byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥
      SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥
      Cipher cipher = Cipher.getInstance("AES");// 创建密码器
      cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式的密码器
      byte[] result = cipher.doFinal(content);
      return result; // 明文

    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}
